import { createStore } from "vuex";
import api from "../api";

export default createStore({
    state:{ //状态声明
        counter:100,
        message:"hello world"
    },
    getters:{ //状态获取
        getCounter(state){
            if (state.counter > 0) {
                return state.counter;
            } else {
                return 0;
            }
        },
        getMessage(state){
            return state.message;
        }
    },
    mutations:{ //状态修改
        addCounter(state,num){
            state.counter += num;
        },
        setMessage(state,message){
            state.message = message;
        }
    },
    actions:{ //状态修改，允许异步操作
        setMessage(context){
            api.getList().then(result=>{ 
                context.commit('setMessage',result.data.data[0]);
            })
        }
    }
})